package com.game
{
	import com.game.components.GameComponent;
	import com.game.events.GameEventType;
	import com.game.interfaces.IGameUpdate;
	import com.update.Update;

	import flash.utils.Dictionary;


	/**
	 *  游戏玩家，怪物，NPC等等基类
	 *
	 */
	public class GameBase extends Update
	{
		public var id : int;

		public var name : String;

		private var comIndex : int = 0;

		private var gameComps : Vector.<GameComponent> = new Vector.<GameComponent>();

		private var frameUpdatables : Array = [];

		private var typeComponent : Dictionary = new Dictionary(false);

		public function GameBase(id : int)
		{
			this.id = id;
			super();
		}

		/**
		 * 为对象添加组件
		 * @param com
		 * @param priority  值越大，越优先处理
		 * index  值越大，处理级别越低
		 *
		 *
		 */
		public function addComponent(comp : GameComponent, priority : int = 0) : void
		{
			if (isDestroy)
				return;
			var index : int = gameComps.indexOf(comp);

			if (index != -1)
			{
				sendError("重复添加组件:" + comp.type);
				return;
			}
			gameComps.push(comp);
			comp.index = comIndex++;

			if (comp is IGameUpdate)
			{
				(comp as IGameUpdate).priority = priority;
				frameUpdatables.push(comp);
				frameUpdatables.sortOn(['priority', 'index'], [Array.DESCENDING, Array.NUMERIC | Array.DESCENDING, Array.NUMERIC]);
			}

			typeComponent[comp.type] = comp;
			comp.owner = this;
			comp.notifyAdded();
		}

		public function removeComponentByType(type : *) : void
		{
			removeComponent(getComponent(type));
		}

		public function removeComponent(comp : GameComponent) : void
		{
			if (isDestroy)
			{
				return;
			}

			if (comp && typeComponent[comp.type] == comp && comp.owner == this)
			{
				var index : int = gameComps.indexOf(comp);

				if (index != -1)
				{
					gameComps.splice(index, 1);
				}
				index = frameUpdatables.indexOf(comp);

				if (index != -1)
				{
					frameUpdatables.splice(index, 1);
				}
				delete typeComponent[comp.type];
				comp.owner = null;

				comp.notifyRemoved(this);
			}
		}

		public function getComponent(type : *) : *
		{
			return typeComponent == null ? null : typeComponent[type];
		}

		private function notifyResetComponent() : void
		{
			for each (var comp : GameComponent in gameComps)
			{
				comp.notifyReset();
			}
		}

		override public function update(elapsedTime : uint) : void
		{
			if (isDestroy)
			{
				return;
			}
			var len : int = frameUpdatables.length;

			for (var i : int = len - 1; i >= 0; i -= 1)
			{
				if (isDestroy)
					return;
				var updatable : IGameUpdate = frameUpdatables[i];

				if (updatable && updatable.checkUpdatable(elapsedTime))
				{
					updatable.update(elapsedTime);
				}
			}
		}

		/**
		 *
		 * @param level   更新的等级,0低级,1中级,2高级
		 * @param priority优先级别
		 *
		 */
		override public function registerd(registerdLevel : int = PRIORITY_LOW, priority : int = 0) : void
		{
			_priority = priority;
			_registerdLevel = registerdLevel;
			sendNotification(GameEventType.ADD_GAME_UPDATE, this);
		}

		override public function unRegisterd() : void
		{
			sendNotification(GameEventType.DEL_GAME_UPDATE, this);
		}

		override public function destroy() : void
		{
			super.destroy();
			var len : int = gameComps.length;

			for (var i : int = len - 1; i >= 0; i--)
			{
				var comp : GameComponent = gameComps[i];

				if (comp)
				{
					comp.destroy();
				}
			}
			gameComps.length = 0;
			frameUpdatables.length = 0;
			typeComponent = null;
		}
	}
}
